package com.fuhui.controller.service;

import com.fuhui.dao.txu.AppMapper;
import com.fuhui.dao.txu.GradeMapper;
import com.fuhui.entity.tjc.Stage;
import com.fuhui.entity.trj.AppRj;
import com.fuhui.entity.trj.AppVersion;
import com.fuhui.entity.txu.App;
import com.fuhui.entity.txu.Grade;
import com.fuhui.util.ReturnJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("schService")
public class SchoolService {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private GradeMapper gradeMapper;

    /**
     * 获取学校可用的应用列表
     * 参数==?iSchoolid=1&iPlace=1&iStatus=1&isPage=0
     * 参数==?iSchoolid=1&iPlace=1&iStatus=1&isPage=1&pNum=1&pLine=10
     * 返回JSON
     * @return
     */
    @RequestMapping("getAppListBySchoolId")
    public Object getAppListBySchoolId(HttpServletRequest request) {
        Object returnObject = new Object();
        List<App> list = new ArrayList<App>();
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> pages = new HashMap<String, Object>();
        List mapList = new ArrayList();
        //判断是否分页
        Integer isPage = Integer.parseInt(request.getParameter("isPage"));
        Integer iSchoolid = Integer.parseInt(request.getParameter("iSchoolid"));
        Integer iPlace = Integer.parseInt(request.getParameter("iPlace"));
        Integer iStatus = Integer.parseInt(request.getParameter("iStatus"));
        if (isPage != 0 || isPage == 1) {
            Integer pNum = Integer.parseInt(request.getParameter("pNum"))- 1;
            Integer pLine = Integer.parseInt(request.getParameter("pLine"));
            pages.put("pageNum", pNum);//pNum
            pages.put("pageLine ", pLine);//pLine
            list = appMapper.getAppListBySchoolId(iSchoolid, iPlace, iStatus, pNum, pLine, isPage);
        } else {
            list = appMapper.getAppListBySchoolId(iSchoolid, iPlace, iStatus, null, null, 0);
        }
        System.out.println("分页查询后的总记录数：" + list.size());
        pages.put("countAll", list.size());
        AppRj appRj = new AppRj();
        AppVersion appVersion = new AppVersion();
        if (list.isEmpty() || list == null) {
            return ReturnJson.returnList(list);
        } else {
            for (App app : list) {
                map.put("type", app.getiType());
                map.put("updateWay", app.getiUway());
                appRj = app.getAppRj();
                appVersion = app.getAppVersion();
                map.put("appId", appRj.getiAppid());
                map.put("name", appRj.getcName());
                map.put("package", appRj.getcPackage());
                map.put("downUrl", appVersion.getcDownurl());
                map.put("version", appVersion.getcVersion());
                map.put("desc", appRj.getcDesc());
                mapList.add(map);
            }
        }
        System.out.println("map==" + map);
        System.out.println("mapList==" + mapList);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("pages", pages);
        map2.put("list", mapList);
        return ReturnJson.returnSuccessMap(map2);
    }

    /**
     *获取学校的年级列表
     * 根据schoolid
     * 参数== ?iSchoolid=1
     * @return
     */
    @RequestMapping("getGradeListBySchoolId")
    public Object getGradeListBySchoolId(HttpServletRequest request){
        /*Integer schoolId = Integer.parseInt(request.getParameter("schoolId"));
        Integer status = Integer.parseInt(request.getParameter("status"));
        String token = request.getParameter("token");//加入token判断是否拥有权限
        */
        Integer iSchoolid = Integer.parseInt(request.getParameter("iSchoolid"));
        Map<String,Object> map = new HashMap<String, Object>();
        Map<String,Object> listMap = new HashMap<String, Object>();
        List mapList = new ArrayList();
        List<Grade> list = gradeMapper.getGradeListBySchoolId(iSchoolid,null);
        if (list == null || list.isEmpty()){
            return ReturnJson.returnFail();
        }
        Grade setGrade = new Grade();
        for (Grade grade : list) {
           /* map.put("idx",grade.getiXgradeid());*/
            map.put("gradeId", grade.getiGradeid());
            map.put("name", grade.getcName());
            map.put("code", grade.getcCode());
            map.put("stageId", grade.getiStageid());
            map.put("orderNo", grade.getiOrderno());
            setGrade.setStage(grade.getStage());
            Stage stage = setGrade.getStage();
            map.put("stageName", stage.getcName());
            mapList.add(map);
        }
        listMap.put("list",mapList);
        return ReturnJson.returnSuccessMap(listMap);
    }
}
